import cgi
import os
import re

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.1')

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.api import users

from models.root_models import rcAdmin

def InstallGCF(userAgent):
  
  regIE = re.compile(r'/.*MSIE.*/')
  regCF = re.compile(r'/.*chromeframe.*/')
  
  if regIE.search(userAgent) == None:
    return False
  else:
    return (regCF.search(userAgent) == None)

class MainHandler(webapp.RequestHandler):
  def get(self):
    displayAdmin = "display: none"
    isAdmin = False
    adminName = ""
    
    user = users.get_current_user()
    if user:
      if users.is_current_user_admin():
        admin = rcAdmin.get_or_insert(key_name=user.nickname(), userAcct=user, userChar=user.nickname())
        adminName = admin.userChar
        displayAdmin = "display: inline;"
        isAdmin = True
   
    template_values = {
    'install_gcf': InstallGCF(self.request.headers['User-Agent']),
    
    'isAdmin': isAdmin,
    'displayAdmin': displayAdmin,
    'user': adminName,
  }
    path = os.path.join(os.path.dirname(__file__), 'templates/launch.html')
    self.response.out.write(template.render(path, template_values))

def main():
  application = webapp.WSGIApplication(
                                        [('/', MainHandler)],
                                        debug=True)
  
  run_wsgi_app(application)


if __name__ == '__main__':
  main()